package net.butfly.bus.util;

import java.net.URL;

import net.butfly.bus.context.Constants;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;


public class LogUtils {
	private static LogUtils instance = new LogUtils();

	private static Log logInst = LogFactory.getLog(Constants.logName);

	private boolean logModeOfConsole = false;

	private InheritableThreadLocal<String> flowNoReg = new InheritableThreadLocal<String>();

	private static String randomSeed;

	private LogUtils() {
		try {
			URL url = Constants.getCoreConfigFile();
			if (url != null) {
				Document doc = new SAXReader().read(url);
				Element logModeEle = (Element) doc.getRootElement().selectSingleNode("log-mode");
				if (logModeEle != null) {
					String logMode = logModeEle.getTextTrim();
					if (logMode != null && logMode.toUpperCase().equals("CONSOLE")) {
						logModeOfConsole = true;
					}
				}
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	// ------------------------------------------------------------------------------
	public static LogUtils getInstance() {
		return instance;
	}

	public static LogUtils getLog() {
		return getInstance();
	}

	// -----------------------------------------------------------------------------
	public boolean isDebugEnabled() {
		return logModeOfConsole || logInst.isDebugEnabled();
	}

	public void info(String message) {
		if (!logModeOfConsole) {
			logInst.info(message);
		} else {
			System.out.println(Constants.logName + "[INFO]---->" + message);
		}
	}

	public void debug(String message) {
		if (!logModeOfConsole) {
			logInst.debug(message);
		} else {
			System.out.println(Constants.logName + "[DEBUG]---->" + message);
		}
	}

	public void error(String message) {
		if (!logModeOfConsole) {
			logInst.error(message);
		} else {
			System.out.println(Constants.logName + "[ERROR]---->" + message);
		}
	}

	public void error(String message, Throwable cause) {
		if (!logModeOfConsole) {
			logInst.error(message, cause);
		} else {
			System.out.println(Constants.logName + "[ERROR]---->" + message);
			if (cause != null) {
				cause.printStackTrace();
			}
		}
	}

	// ------------------------------------------------------------------------------
	public String getFlowNo(String... keys) {
		if (flowNoReg.get() == null) {
			flowNoReg.set(keys.length > 0 ? keys[0] : getRandomId(7));
		} else if (keys.length > 0) {
			String key = keys[0];
			int at = key.indexOf("@");
			int at1 = flowNoReg.get().indexOf("@");
			if (at != -1 && at1 != -1) {
				key = key.substring(at + 1);
				key = flowNoReg.get().substring(0, at1) + "@" + key;
				flowNoReg.set(key);
			}
		}
		return flowNoReg.get();
	}

	public void clearFlowNo() {
		flowNoReg.remove();
	}

	// ------------------------------------------------------------------------------
	public static synchronized String getRandomId(int _length) {
		String rst = "";
		if (randomSeed == null) {
			randomSeed = "";
			for (int i = 0; i < 10; i++) {
				randomSeed = randomSeed + i;
			}
		}
		for (int i = 0; i < _length; i++) {
			int mathInt;
			mathInt = (int) (Math.random() * randomSeed.length());
			rst = rst + randomSeed.charAt(mathInt);
		}
		return rst;
	}
	// ------------------------------------------------------------------------------

}
